package com.gz.tomcat;

import com.gz.servlet_my.GzCalServlet;
import com.gz.servlet_my.GzHttpServlet;
import com.gz.tomcat.handler.RequestHandler;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.xml.transform.sax.SAXSource;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class GzTomcatV3 {

    //1.存放容器
    //ConcurrentHashMap
    //servletMapping
    //key            |    value
    //ServletName    |    对应的实例
    public static final ConcurrentHashMap<String, GzHttpServlet> servletMapping =
            new ConcurrentHashMap<>();

    //2.servletUrlMapping
    //key            |    value
    //url-pattern    |    ServletName
    public static final ConcurrentHashMap<String, String> servletUrlMapping =
            new ConcurrentHashMap<>();

    public static void main(String[] args) {
        GzTomcatV3 gzTomcatV3 = new GzTomcatV3();
        gzTomcatV3.init();
        gzTomcatV3.run();
    }

    //启动GzTomcatV3容器
    public void run(){
        try {
            ServerSocket serverSocket = new ServerSocket(8080);
            System.out.println("========GzTomcatV3在8080监听========");
            int count=1;
            while(!serverSocket.isClosed()){
                Socket socket = serverSocket.accept();
                RequestHandler requestHandler = new RequestHandler(socket);
                System.out.println("当前线程= Thread"+(count++));
                new Thread(requestHandler).start();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
    //对两个容器进行初始化
    public void init() {
        //读取web.xml -> dom4j
        //得到web.xml的工作路径
        ///E:/Java/java_code/JavaWeb/java-web/tomcat_maven/target/classes/
        String path = GzCalServlet.class.getResource("/").getPath();
//        System.out.println(path);
        //使用dom4j完成读取
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(new File(path + "web.xml"));
            //org.dom4j.tree.DefaultDocument@377dca04
//            System.out.println(document);
            //获取根元素
            Element rootElement = document.getRootElement();
            List<Element> elements = rootElement.elements();
            for (Element element : elements) {
                if ("servlet".equalsIgnoreCase(element.getName())) {
                    //Servlet
                    //System.out.println("这是servlet: ");
                    //使用反射将该Servlet实例servletMapping中

                    //<servlet-name>GzCalServlet</servlet-name>
                    Element servletName = element.element("servlet-name");
                    //<servlet-class>com.gz.servlet_my.GzCalServlet</servlet-class>
                    Element servletClass = element.element("servlet-class");
                    //拿到一个文本内容，一个对象实例并放入servletMapping
                    servletMapping.put(servletName.getText(),
                            (GzHttpServlet) Class.forName(servletClass.getText().trim()).newInstance());

                } else if ("servlet-mapping".equalsIgnoreCase(element.getName())) {
                    //servletUrlMapping
                    //System.out.println("这是servlet-mapping: ");
                    Element servletName = element.element("servlet-name");
                    Element urlPattern = element.element("url-pattern");
                    servletUrlMapping.put(urlPattern.getText(),servletName.getText());

                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println(servletMapping);
        System.out.println(servletUrlMapping);

    }
}
